<template>
  <div>
    <select-user
      :contains-temp-clinic-person="containsTempClinicPerson"
      @create="handleCreate"
      @patient="handlePatient"
      @market="handleMarket"
      @submit="handleAppointment"
    ></select-user>
    <patient-editor-panel
      v-model="patientEditorPanel.visible"
      :market="patientEditorPanel.market"
      :patient="patientEditorPanel.patient"
      :phone-number="patientEditorPanel.phoneNumber"
      @submit="handleSubmit"
    ></patient-editor-panel>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import dayjs from 'dayjs'
import { addAppointment } from '@/api/services/appointment'
import { listStaff } from '@/api/services/common'
import { SAVE_APPOINTMENT_PANEL } from '@/store/modules/userMatcher'

export default {
  name: 'UserMatcherResult',
  components: {
    PatientEditorPanel: () => import('@/components/yiya/PatientEditorPanel'),
    SelectUser: () => import('./SelectUser/index'),
  },
  props: {
    containsTempClinicPerson: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      patientEditorPanel: {
        visible: false,
      },
      doctor: {},
      tcList: [],
    }
  },
  computed: {
    ...mapState({
      clinicName: (state) => state.clinic.clinicName,
      clinicId: (state) => state.clinic.clinicId,
      userName: (state) => state.user.name,
    }),
  },
  mounted() {
    this.getDoctor()
    this.getTcList()
  },
  methods: {
    /**
     * 获取医生列表
     */
    async getDoctor() {
      const { data } = await listStaff({
        deptId: +this.clinicId,
        postname: '医生',
      })
      data.forEach((item) => {
        if (item.nickName === '未指定医生') {
          this.doctor = item
        }
      })
    },
    /**
     * 获取TC列表
     */
    async getTcList() {
      const { data } = await listStaff({
        deptId: +this.clinicId,
        postname: 'TC',
      })
      this.tcList = data
    },
    handleCreate(phoneNumber) {
      this.patientEditorPanel = {
        visible: true,
        phoneNumber,
      }
    },
    handlePatient(patient) {
      this.patientEditorPanel = {
        visible: true,
        patient,
      }
    },
    handleMarket(market) {
      this.patientEditorPanel = {
        visible: true,
        market,
      }
    },
    async handleSubmit(patient) {
      const { firstTc } = patient
      let tcId, tcName
      this.tcList.forEach((item) => {
        if (item.nickName === firstTc) {
          tcId = item.userId
          tcName = item.nickName
        }
      })
      const startReservationTime = dayjs(
        Math.ceil(+new Date() / (15 * 60 * 1000)) * (15 * 60 * 1000)
      ).format('YYYY-MM-DD HH:mm:ss')
      const endReservationTime = dayjs(startReservationTime)
        .add(1, 'hour')
        .format('YYYY-MM-DD HH:mm:ss')
      const formData = {
        clinicPersonId: patient.id,
        clinicPersonName: patient.name,
        doctorId: this.doctor.userId,
        doctorName: this.doctor.nickName,
        tcId,
        tcName,
        reservationDate: dayjs().format('YYYY-MM-DD'),
        startReservationTime,
        endReservationTime,
        clinicName: this.clinicName,
        clinicId: +this.clinicId,
        createBy: this.userName,
        appointmentStatus: 3,
        onClinicTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        personStatus: '新客首次',
      }
      await addAppointment({ ...formData }) //, appointmentStatus: 1
      this.msgSuccess('预约成功')
      this.$emit('update')
    },
    handleAppointment(patient, type) {
      const startReservationTime = dayjs(
        Math.ceil(+new Date() / (15 * 60 * 1000)) * (15 * 60 * 1000)
      ).format('YYYY-MM-DD HH:mm:ss')
      const endReservationTime = dayjs(startReservationTime)
        .add(1, 'hour')
        .format('YYYY-MM-DD HH:mm:ss')
      this.$store.commit({
        type: `userMatcher/${SAVE_APPOINTMENT_PANEL}`,
        value: {
          visible: true,
          data: {
            reservationDate: dayjs().format('YYYY-MM-DD'),
            startReservationTime,
            endReservationTime,
            clinicName: this.clinicName,
            clinicId: +this.clinicId,
            createBy: this.userName,
          },
          patient: {
            ...patient,
            personStatus: type,
          },
          type: type === '老客' ? '重咨' : '复诊',
        },
      })
    },
  },
}
</script>

<style scoped></style>
